package com.juli.school.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.AnswerDetails;
import com.juli.school.dao.entity.Stu;
import com.juli.school.dao.mapper.AnswerDetailsMapper;
import com.juli.school.dao.mapper.StuMapper;
import com.juli.school.service.bo.AnswerDetailsBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;
import com.juli.school.utils.dao.base.Page;

/**
 *  
 * @author: hantao
 * @since: 2017年11月13日 
 */
@Service
public class AnswerDetailsService {
	private static final Logger logger = LoggerFactory.getLogger(AnswerDetails.class);
	
	@Resource
	AnswerDetailsMapper answerDetailsMapper;
	
	@Resource
	private StuMapper stuMapper;

	public CommonResp<Boolean> add(AnswerDetailsBo answerDetailsBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (answerDetailsBo.getStuId() == null) {
				commonResp.fail("学生ID不可为空");
				return commonResp;
			}
			if(answerDetailsBo.getSnStamp() == null) {
				commonResp.fail("心理测试编号不可为空");
				return commonResp;
			}
			// 判断学生存在否
			Stu stu = stuMapper.getByStuId(answerDetailsBo.getStuId());
			if (stu == null) {
				commonResp.fail(String.format("学生%s不存在", answerDetailsBo.getStuId()));
				return commonResp;
			}
			// 设置 起止日期
			answerDetailsMapper.insert(answerDetailsBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增答题情况信息异常", e);
			commonResp.exception("新增答题情况信息异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> update(AnswerDetailsBo answerDetailsBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if(Strings.isNullOrEmpty(answerDetailsBo.getSnStamp())) {
				commonResp.fail("心理测试不可为空");
				return commonResp;
			}
			if (answerDetailsBo.getStuId() == null) {
				commonResp.fail("学生ID不可为空");
				return commonResp;
			}
			answerDetailsMapper.update(answerDetailsBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新答题情况信息异常", e);
			commonResp.exception("更新答题情况信息异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> delete(String snStamp, String stuId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (StringUtils.isEmpty(snStamp) || StringUtils.isEmpty(stuId)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			AnswerDetails answerDetails = new AnswerDetails();
			answerDetails.setSnStamp(snStamp);
			answerDetails.setStuId(stuId);
			answerDetailsMapper.deleteBySnStampAndstuId(answerDetails);
			commonResp.success();
		} catch (Exception e) {
			logger.error("删除答题情况信息异常", e);
			commonResp.exception("删除答题情况信息异常");
		}
		return commonResp;
	}

	public CommonResp<AnswerDetails> detail(String snStamp, String stuId) {
		CommonResp<AnswerDetails> commonResp = new CommonResp<AnswerDetails>();
		try {
			if(Strings.isNullOrEmpty(snStamp)) {
				commonResp.fail("心理测试不可为空");
				return commonResp;
			}
			if (Strings.isNullOrEmpty(stuId)) {
				commonResp.fail("学生ID不可为空");
				return commonResp;
			}
			AnswerDetails answerDetailsBo = new AnswerDetails();
			answerDetailsBo.setSnStamp(snStamp);
			answerDetailsBo.setStuId(stuId);
			AnswerDetails answerDetails = answerDetailsMapper.get(answerDetailsBo);
			if (answerDetails == null) {
				commonResp.exception("没有获取到信息");
				return commonResp;
			}
			BeanUtils.copyProperties(answerDetailsBo, answerDetails);
			commonResp.setData(answerDetails);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取答题情况信息发生异常", e);
			commonResp.exception("获取答题情况信息发生异常");
		}
		return commonResp;
	}

	public CommonResp<List<AnswerDetailsBo>> listPage(String snStamp,
			Integer pageNo) {
		CommonResp<List<AnswerDetailsBo>> commonResp = new CommonResp<List<AnswerDetailsBo>>();
		try {
			if (Strings.isNullOrEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			AnswerDetailsBo req = new AnswerDetailsBo();
			Page page = new Page();
			page.setCurrentPage(pageNo==null?0:Integer.valueOf(pageNo));
			page.setShowCount(20);
			req.setPage(page);
			List<AnswerDetails> listPage = answerDetailsMapper.listPage(req);
			List<AnswerDetailsBo> data = Lists.newArrayList();
			for (AnswerDetails answerDetails : listPage) {
				AnswerDetailsBo answerDetailsBo = new AnswerDetailsBo();
				BeanUtils.copyProperties(answerDetailsBo, answerDetails);
				data.add(answerDetailsBo);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (Exception e) {
			logger.error("获取心理测试答题情况列表异常", e);
			commonResp.exception("获取心理测试答题情况列表异常");
		}
		return commonResp;
	}
	
	
}
